package com.ljkj.cordial.chat.widget;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aigestudio.wheelpicker.WheelPicker;
import com.ljkj.cordial.chat.R;
import com.nic.view.widget.CommonPopupWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TimeChoosePop extends CommonPopupWindow {
    private List<String> getList() {
        List<String> list = new ArrayList<>();
        for (int i = 6; i < 10; i++) {
            list.add(i + "");
        }
        for (int i = 0; i < 6; i++) {
            list.add(i + "");
        }
        return list;
    }

    private List<String> getPickList() {
        List<String> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            list.add(i + "");
        }
        return list;
    }

    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.wheel_t1)
    WheelPicker wheelT1;
    @BindView(R.id.wheel_t2)
    WheelPicker wheelT2;
    @BindView(R.id.wheel_t3)
    WheelPicker wheelT3;


    private int t1;
    private int t2 = 6;
    private int t3;

    public TimeChoosePop(Context c) {
        super(c, R.layout.dialog_read_time_pop, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ButterKnife.bind(this, contentView);
//        setWheel(0, wheelT1, new WheelView.OnWheelViewListener() {
////            @Override
////            public void onSelected(int selectedIndex, String item) {
////                super.onSelected(selectedIndex, item);
////                t1 = Integer.parseInt(item);
////            }
////        });
////        setWheel(6, wheelT2, new WheelView.OnWheelViewListener() {
////            @Override
////            public void onSelected(int selectedIndex, String item) {
////                super.onSelected(selectedIndex, item);
////                t2 = Integer.parseInt(item);
////            }
////        });
////        setWheel(0, wheelT3, new WheelView.OnWheelViewListener() {
////            @Override
////            public void onSelected(int selectedIndex, String item) {
////                super.onSelected(selectedIndex, item);
////                t3 = Integer.parseInt(item);
////            }
////        });
        wheelT1.setData(getPickList());
        wheelT1.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {

                getValue();
            }
        });
        wheelT2.setData(getList());
        wheelT2.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                getValue();
            }
        });


        wheelT3.setData(getPickList());
        wheelT3.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                getValue();
            }
        });

//        getPopupWindow().setOutsideTouchable(true);

    }

    private void getValue() {
        t1 = Integer.parseInt((String) wheelT1.getData().get(wheelT1.getCurrentItemPosition()));
        t2 = Integer.parseInt((String) wheelT2.getData().get(wheelT2.getCurrentItemPosition()));
        t3 = Integer.parseInt((String) wheelT3.getData().get(wheelT3.getCurrentItemPosition()));
    }

//    private void setWheel(int select, WheelView wheelT, WheelView.OnWheelViewListener listener) {
////        wheelT.setWeight(1);
//        wheelT.setOffset(3);
//        wheelT.setItems(getList());
//        wheelT.setSeletion(select);
//        wheelT.setOnWheelViewListener(listener);
//    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        getPopupWindow().dismiss();
        switch (view.getId()) {
            case R.id.tv_cancel:
                break;
            case R.id.tv_confirm:
                if (onConfirmListener != null) {
                    onConfirmListener.onConfirm(Integer.parseInt(t1 + "" + t2 + "" + t3));
                }
                break;
        }
    }

    public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
    }

    private OnConfirmListener onConfirmListener;

    public interface OnConfirmListener {
        void onConfirm(int time);
    }
}
